﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Party.PartyComponents.MilitiaPartyComponent
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Party.PartyComponents
{
  public class MilitiaPartyComponent : PartyComponent
  {
    [CachedData]
    private TextObject _cachedName;

    internal static void AutoGeneratedStaticCollectObjectsMilitiaPartyComponent(
      object o,
      List<object> collectedObjects)
    {
      ((PartyComponent) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Settlement);
    }

    internal static object AutoGeneratedGetMemberValueSettlement(object o)
    {
      return (object) ((MilitiaPartyComponent) o).Settlement;
    }

    [SaveableProperty(1)]
    public Settlement Settlement { get; private set; }

    public override Hero PartyOwner => this.Settlement.OwnerClan.Leader;

    public override TextObject Name
    {
      get
      {
        if (this._cachedName == null)
        {
          this._cachedName = GameTexts.FindText("str_militia_name");
          this._cachedName.SetTextVariable("SETTLEMENT_NAME", this.Settlement.Name);
        }
        return this._cachedName;
      }
    }

    public override Settlement HomeSettlement => this.Settlement;

    public static MobileParty CreateMilitiaParty(string stringId, Settlement settlement)
    {
      MobileParty party = MobileParty.CreateParty("militias_of_" + stringId + "_aaa1", (PartyComponent) new MilitiaPartyComponent(settlement), (PartyComponent.OnPartyComponentCreatedDelegate) (mobileParty => (mobileParty.PartyComponent as MilitiaPartyComponent).InitializeMilitiaPartyProperties(mobileParty, settlement)));
      EnterSettlementAction.ApplyForParty(party, settlement);
      return party;
    }

    protected override void OnInitialize() => this.Settlement.MilitiaPartyComponent = this;

    protected override void OnFinalize()
    {
      this.Settlement.MilitiaPartyComponent = (MilitiaPartyComponent) null;
    }

    public override void ClearCachedName() => this._cachedName = (TextObject) null;

    private void InitializeMilitiaPartyProperties(MobileParty mobileParty, Settlement settlement)
    {
      PartyTemplateObject militiaPartyTemplate = settlement.Culture.MilitiaPartyTemplate;
      mobileParty.InitializeMobilePartyAtPosition(militiaPartyTemplate, settlement.GatePosition);
      mobileParty.Party.SetVisualAsDirty();
      mobileParty.Ai.DisableAi();
      mobileParty.Aggressiveness = 0.0f;
    }

    protected internal MilitiaPartyComponent(Settlement settlement) => this.Settlement = settlement;
  }
}
